package com.baozun.houji.me.fragment

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.activity.WebContainerActivity
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseFragment
import com.baozhun.mall.common.ext.checkLogin
import com.baozhun.mall.common.model.bean.ImageViewInfo
import com.baozhun.mall.common.model.enums.OrderStatus
import com.baozhun.mall.common.util.CacheUtil
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.dialog.ChooseCustomerServiceDialog
import com.baozun.houji.me.R
import com.baozun.houji.me.activity.*
import com.baozun.houji.me.databinding.FragmentMeBinding
import com.baozun.houji.me.model.enums.CollectTabType
import com.baozun.houji.me.viewmodel.MeViewModel
import com.previewlibrary.GPreviewBuilder
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener


/**
 * @Title：Mefragment
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/4
 * @Version：1.0.0
 */
class MeFragment : BaseFragment<MeViewModel, FragmentMeBinding>(), OnRefreshListener {

    override fun initView(savedInstanceState: Bundle?) {
        mDataBinding.refreshLayout.setOnRefreshListener(this)
    }

    override fun initData() {
        super.initData()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        checkLogin {
            when (v?.id) {
                R.id.iv_avatar -> {
                    GPreviewBuilder.from(this) //activity实例必须
                        .setSingleData(ImageViewInfo(CacheUtil.getUser().photo.toString()).apply {
                            mDataBinding.ivAvatar.getGlobalVisibleRect(bounds)
                        }).setCurrentIndex(0).setType(GPreviewBuilder.IndicatorType.Dot) //指示器类型
                        .start() //启动
                } //头像预览
                R.id.iv_set -> SetActivity.start(requireContext()) //设置
                R.id.all_order_tv -> ARouter.getInstance().build(RoutePath.Order.ORDER_LIST).withSerializable(Constants.Order.ORDER_STATUS, OrderStatus.ALL).navigation()  //全部订单
                R.id.ll_wait_pay -> ARouter.getInstance().build(RoutePath.Order.ORDER_LIST).withSerializable(Constants.Order.ORDER_STATUS, OrderStatus.WAIT_DELIVERY).navigation()  //待支付
                R.id.ll_wait_delivery -> ARouter.getInstance().build(RoutePath.Order.ORDER_LIST).withSerializable(Constants.Order.ORDER_STATUS, OrderStatus.WAIT_SEND).navigation()  //待配送
                R.id.ll_wait_receive -> ARouter.getInstance().build(RoutePath.Order.ORDER_LIST).withSerializable(Constants.Order.ORDER_STATUS, OrderStatus.WAIT_RECEIVE).navigation()  //待收货
                R.id.ll_refund -> ARouter.getInstance().build(RoutePath.Order.ORDER_LIST).withSerializable(Constants.Order.ORDER_STATUS, OrderStatus.AFTER_SALE).navigation()  //售后
                R.id.tv_my_collect -> MyCollectionActivity.start(mActivity, CollectTabType.COLLECT) //我的收藏
                R.id.tv_browsing_history -> MyCollectionActivity.start(mActivity, CollectTabType.BROWSE) //浏览记录
                R.id.tv_service -> {
                    ChooseCustomerServiceDialog(mActivity).show()
                }

                R.id.tv_about -> WebContainerActivity.start(mActivity, Constants.Url.ABOUT_US, mActivity.getString(com.baozhun.mall.res.R.string.about_us)) //关于我们
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getUserInfo()
    }

    override fun createObserver() {
        mViewModel.mUserInfo.observe(this) {
            mDataBinding.refreshLayout.finishRefresh()
            CacheUtil.setUser(it)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.getUserInfo()
    }
}